package cn.stylefeng.guns.modular.business.frequency.orderfinishrecorddetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecorddetail.entity.OrderFinishRecordDetail;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecorddetail.mapper.OrderFinishRecordDetailMapper;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecorddetail.model.param.OrderFinishRecordDetailParam;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecorddetail.service.OrderFinishRecordDetailService;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecorddetail.enums.OrderFinishRecordDetailExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单交付记录详情表 服务实现类
 *
 * @author yujie
 * @date 2023/05/30 18:11
 */
@Service
public class OrderFinishRecordDetailServiceImpl extends ServiceImpl<OrderFinishRecordDetailMapper, OrderFinishRecordDetail> implements OrderFinishRecordDetailService {

    @Override
    public PageResult<OrderFinishRecordDetail> page(OrderFinishRecordDetailParam orderFinishRecordDetailParam) {

        // 构造条件
        LambdaQueryWrapper<OrderFinishRecordDetail> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderFinishRecordDetail> list(OrderFinishRecordDetailParam orderFinishRecordDetailParam) {

        // 构造条件
        LambdaQueryWrapper<OrderFinishRecordDetail> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderFinishRecordDetailParam orderFinishRecordDetailParam) {

        // 将dto转为实体
        OrderFinishRecordDetail orderFinishRecordDetail = new OrderFinishRecordDetail();
        BeanUtil.copyProperties(orderFinishRecordDetailParam, orderFinishRecordDetail);

        this.save(orderFinishRecordDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderFinishRecordDetailParam orderFinishRecordDetailParam) {
        this.removeById(orderFinishRecordDetailParam.getId());
    }

    @Override
    public void edit(OrderFinishRecordDetailParam orderFinishRecordDetailParam) {

        // 根据id查询实体
        OrderFinishRecordDetail orderFinishRecordDetail = this.queryOrderFinishRecordDetail(orderFinishRecordDetailParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderFinishRecordDetailParam, orderFinishRecordDetail);

        this.updateById(orderFinishRecordDetail);
    }

    @Override
    public OrderFinishRecordDetail detail(OrderFinishRecordDetailParam orderFinishRecordDetailParam) {
        return this.queryOrderFinishRecordDetail(orderFinishRecordDetailParam);
    }



    /**
     * 获取订单交付记录详情表
     *
     * @author yujie
     * @since 2023/05/30 18:11
     */
    private OrderFinishRecordDetail queryOrderFinishRecordDetail(OrderFinishRecordDetailParam orderFinishRecordDetailParam) {
        OrderFinishRecordDetail orderFinishRecordDetail = this.getById(orderFinishRecordDetailParam.getId());
        if (ObjectUtil.isEmpty(orderFinishRecordDetail)) {
            throw new ServiceException(OrderFinishRecordDetailExceptionEnum.NOT_EXISTED);
        }
        return orderFinishRecordDetail;
    }
}